/**
 * Copyright (c) qzsoft All rights reserved.
 *
 * qzsoft.cn
 *
 * 版权所有，侵权必究！
 */

package com.qzsoft.system.modules.sys.message.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qzsoft.jeemis.common.service.BaseService;
import com.qzsoft.jeemis.common.utils.ConvertDictUtils;
import com.qzsoft.jeemis.platform.security.user.UserDetail;
import com.qzsoft.system.modules.permission.dept.dao.SysDeptMapper;
import com.qzsoft.system.modules.sys.message.dao.SysMessageMapper;
import com.qzsoft.system.modules.sys.message.dto.SysFlowDataDTO;
import com.qzsoft.system.modules.sys.message.service.SysMessageService;
import com.qzsoft.system.modules.sys.notice.dao.SysNoticeUserMapper;
import com.qzsoft.system.modules.sys.notice.dto.SysNoticeDto;
import com.qzsoft.system.modules.sys.notice.entity.SysNoticeUserEntity;
import com.qzsoft.system.repository.sys.entity.SysDeptEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
/**
 * 系统用户
 * 
 * @author
 */
@Service
public class SysMessageServiceImpl extends BaseService implements SysMessageService {

	@Autowired
	private SysMessageMapper sysMessageMapper;

	@Autowired
	private SysDeptMapper sysDeptMapper;

	@Autowired
	private SysNoticeUserMapper sysNoticeUserMapper;

	@Override
	public IPage<SysFlowDataDTO> getNotAndHandleFlowData(Page page) {
		QueryWrapper<SysFlowDataDTO> queryWrapper1 = Wrappers.query();
		queryWrapper1.eq("iden.USER_ID_", this.basegetLoginUser().getUsername());
		QueryWrapper<SysFlowDataDTO> queryWrapper2 = Wrappers.query();
		queryWrapper2.eq("hiTask.auditor_id", this.basegetLoginUser().getUsername());
		IPage<SysFlowDataDTO> list = sysMessageMapper.getNotAndHandleFlowData(page, queryWrapper1,queryWrapper2);
		return list;
	}

	@Override
	public IPage<SysFlowDataDTO> getNotHandleFlowData(Page page) {
		QueryWrapper<SysFlowDataDTO> queryWrapper = Wrappers.query();
		queryWrapper.eq("iden.USER_ID_", this.basegetLoginUser().getUsername());
		IPage<SysFlowDataDTO> list = sysMessageMapper.getNotHandleFlowData(page,queryWrapper);
		return list;
	}

	/**
	 * 更新通知状态
	 * @return
	 */
	@Override
	public void updateReadStatus(SysNoticeDto dto){
		SysNoticeUserEntity sysNoticeUserEntity = sysNoticeUserMapper.selectById(dto.getId());
		if(sysNoticeUserEntity != null){
			sysNoticeUserEntity.setReadFlag(true);
			sysNoticeUserMapper.updateById(sysNoticeUserEntity);
		}
		// 当用户存在与多个机构时候 当前机构已读时，则下级机构变为已读
//		List<String> deptIdList;
//		SysNoticeUserEntity sysNoticeUserEntity = sysNoticeUserMapper.selectById(dto.getId());
//		List<String> deptIdList;
//		if(sysNoticeUserEntity != null){
//			SysDeptEntity sysDeptEntity = sysDeptMapper.selectById(sysNoticeUserEntity.getDeptId());
//			dto.setUserId(sysNoticeUserEntity.getUserId().toString());
//			dto.setDeptCode(sysDeptEntity.getCode());
//			deptIdList = sysMessageMapper.getUpdateDeptIdList(dto);
//			if(deptIdList != null && deptIdList.size() > 0){
//				// 更新为已读
//				UpdateWrapper<SysNoticeUserEntity> updateNoticeUser = new UpdateWrapper<>();
//				updateNoticeUser.set("read_flag",1).eq("user_id",sysNoticeUserEntity.getUserId()).eq("del_flag","0").eq("notice_id",sysNoticeUserEntity.getNoticeId()).in("dept_id",deptIdList.toArray());
//				sysNoticeUserMapper.update(new SysNoticeUserEntity(),updateNoticeUser);
//			}
//		}
	}

}
